﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Diagnostics;

namespace Bukovics.Workflow.Hosting
{
    public sealed class FileTools
    {
        private FileTools()
        {
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="mainPath"></param>
        /// <param name="instanceId"></param>
        /// <param name="typ"></param>
        /// <returns></returns>
        public static object XmlDeserializeObject(string mainPath, Guid instanceId, Type typ)
        {
            string fullPath = Path.GetFullPath(mainPath);
            if (!Directory.Exists(fullPath))
                Directory.CreateDirectory(fullPath);

            string fspath = Path.Combine(fullPath, instanceId.ToString() + ".xsa");
            if (File.Exists(fspath))
            {
                using (StreamReader writer = new StreamReader(fspath, false))
                {
                    XmlSerializer xs = new XmlSerializer(typ);
                    return xs.Deserialize(writer);
                }
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="mainPath"></param>
        /// <param name="instanceId"></param>
        /// <param name="o"></param>
        public static void XmlSerializeObject(string mainPath, Guid instanceId, object o)
        {
            string fullPath = Path.GetFullPath(mainPath);
            if (!Directory.Exists(fullPath))
                Directory.CreateDirectory(fullPath);

            string fspath = Path.Combine(fullPath, instanceId.ToString() + ".xsa");
            using (StreamWriter writer = new StreamWriter(fspath, false))
            {
                XmlSerializer xs = new XmlSerializer(o.GetType());
                xs.Serialize(writer, o);
            }
        }
        public static List<Guid> GetAllWorkflows(string mainPath)
        {
            List<Guid> list = new List<Guid>();
            DirectoryInfo dirInfo = new DirectoryInfo(mainPath);
            if (dirInfo.Exists)
            {
                string name;
                foreach (FileInfo f6o in dirInfo.GetFiles("*.xact"))
                {
                    name = Path.GetFileNameWithoutExtension(f6o.Name);
                    list.Add(new Guid(name));
                }
            }

            return list;
        }
        public static void SavePendingWorkItem(string mainPath, PendingWorkItem item)
        {
            if (!Directory.Exists(mainPath))
                Directory.CreateDirectory(mainPath);

            using (StreamWriter writer = new StreamWriter(Path.Combine(mainPath, item.InstanceId.ToString() + ".xact"), false))
            {
                XmlSerializer xs = new XmlSerializer(item.GetType());
                xs.Serialize(writer, item);
            }
        }
        public static void BakPendingWorkItem(string mainPath, Guid instanceId)
        {
            string fp = Path.Combine(mainPath, instanceId + ".xact");
            try
            {
                if (File.Exists(fp))
                {
                    File.Move(fp, fp + ".bak");
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("FileTools.RmPendingWorkItem:" + ex.ToString());
            }
        }
        public static void LoadPendingWorkItem(string mainPath, Guid instanceId, out PendingWorkItem item)
        {
            item = null;
            using (StreamReader read = new StreamReader(Path.Combine(mainPath, instanceId + ".xact")))
            {
                XmlSerializer xs = new XmlSerializer(typeof(PendingWorkItem));
                object o = xs.Deserialize(read);
                item = o as PendingWorkItem;
            }
        }

        public static void LoadActivity(string mainPath, Guid instanceId, out byte[] buffer)
        {
            buffer = new byte[0];
            PendingWorkItem item = null;
            LoadPendingWorkItem(mainPath, instanceId, out item);
            if (item != null)
                buffer = item.SerializedActivity;


        }
    }
}
